
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[Goals_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[Goals_Insert]

IF OBJECT_ID(N'[dbo].[Goals_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[Goals_Update]

IF OBJECT_ID(N'[dbo].[Goals_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[Goals_Delete]

IF OBJECT_ID(N'[dbo].[Goals_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[Goals_Select]

--endregion

GO


--region [dbo].[Goals_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Goals_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdGoal int = null,
	@IdMatch int = null,
	@IdClub int = null,
	@IdPlayer int = null,
	@GoalType nvarchar(50) = null,
	@Minutes nvarchar(50) = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[Goals]
					WHERE
					(
						(@IdGoal is null OR [Goals].[IdGoal] = @IdGoal)
						AND (@IdMatch is null OR [Goals].[IdMatch] = @IdMatch)
						AND (@IdClub is null OR [Goals].[IdClub] = @IdClub)
						AND (@IdPlayer is null OR [Goals].[IdPlayer] = @IdPlayer)
						AND (@GoalType is null OR [Goals].[GoalType] like @GoalType)
						AND (@Minutes is null OR [Goals].[Minutes] like @Minutes)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[Goals].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdGoal' and @SortType = 1 THEN [Goals].[IdGoal] END ASC,
						CASE WHEN @SortBy = 'IdGoal' and @SortType = 0 THEN [Goals].[IdGoal] END DESC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 1 THEN [Goals].[IdMatch] END ASC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 0 THEN [Goals].[IdMatch] END DESC,
						CASE WHEN @SortBy = 'IdClub' and @SortType = 1 THEN [Goals].[IdClub] END ASC,
						CASE WHEN @SortBy = 'IdClub' and @SortType = 0 THEN [Goals].[IdClub] END DESC,
						CASE WHEN @SortBy = 'IdPlayer' and @SortType = 1 THEN [Goals].[IdPlayer] END ASC,
						CASE WHEN @SortBy = 'IdPlayer' and @SortType = 0 THEN [Goals].[IdPlayer] END DESC,
						CASE WHEN @SortBy = 'GoalType' and @SortType = 1 THEN [Goals].[GoalType] END ASC,
						CASE WHEN @SortBy = 'GoalType' and @SortType = 0 THEN [Goals].[GoalType] END DESC,
						CASE WHEN @SortBy = 'Minutes' and @SortType = 1 THEN [Goals].[Minutes] END ASC,
						CASE WHEN @SortBy = 'Minutes' and @SortType = 0 THEN [Goals].[Minutes] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[Goals]
			Where 
			(
				(@IdGoal is null OR [Goals].[IdGoal] = @IdGoal)
				AND (@IdMatch is null OR [Goals].[IdMatch] = @IdMatch)
				AND (@IdClub is null OR [Goals].[IdClub] = @IdClub)
				AND (@IdPlayer is null OR [Goals].[IdPlayer] = @IdPlayer)
				AND (@GoalType is null OR [Goals].[GoalType] like @GoalType)
				AND (@Minutes is null OR [Goals].[Minutes] like @Minutes)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[Goals_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[Goals_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Goals_Insert]
	@IdGoal int OUTPUT,
	@IdMatch int,
	@IdClub int,
	@IdPlayer int,
	@GoalType nvarchar(50),
	@Minutes nvarchar(50)

AS


INSERT INTO [dbo].[Goals] 
(
	[IdMatch],
	[IdClub],
	[IdPlayer],
	[GoalType],
	[Minutes]
)
VALUES 
(
	@IdMatch,
	@IdClub,
	@IdPlayer,
	@GoalType,
	@Minutes
)

SET @IdGoal = SCOPE_IDENTITY()

--end [dbo].[Goals_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[Goals_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Goals_Update]
	@IdGoal int,
	@IdMatch int,
	@IdClub int,
	@IdPlayer int,
	@GoalType nvarchar(50),
	@Minutes nvarchar(50)
AS


UPDATE [dbo].[Goals] SET
	[IdMatch] = @IdMatch,
	[IdClub] = @IdClub,
	[IdPlayer] = @IdPlayer,
	[GoalType] = @GoalType,
	[Minutes] = @Minutes
WHERE
	[IdGoal] = @IdGoal

--end [dbo].[Goals_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[Goals_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Goals_Delete]
	@IdGoal int
AS


DELETE FROM [dbo].[Goals]
WHERE
(
	[IdGoal] = @IdGoal
)

--end [dbo].[Goals_Delete]
--endregion

GO
--=========================================================================================--

